﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;
using System.Collections.Generic;

namespace UI
{
    public class UICopySweep :Page
    {
        //扫荡
        public UILabel sweepSubmitBtnTxt;
        public UILabel sweepTitleTxt;
        
        public GameObject sweepItem;//sweep panel
        public UIGrid sweepItemGrid;
        public UIScrollView sweepScrollView;

        //public int times;//第几次

        public TweenPosition titlePosition;
        public TweenAlpha titleAlpha;

        public TweenScale bodyScale;
        public TweenAlpha rotateAlpha;

        //public GameObject btn;

        public int count = 0;
        public string levelId;
        public string chapterId;

        public AnimationCurve curve;
        public Vector3 pos = Vector3.zero;

        private int openedLevel;

        public GameObject submitBtn;
        public GameObject stopBtn;

        void Awake()
        {
            sweepSubmitBtnTxt.text = TableTextBlo.GetText( "submit" );
            sweepTitleTxt.text = TableTextBlo.GetText( "copy_sweep_title" );

            pos = sweepItemGrid.transform.localPosition;
        }    

        protected override void DoOpen()
        {
            //btn.SetActive(false);
            submitBtn.SetActive(false);
            stopBtn.SetActive(false);
            needStop = false;
            //var evt = GetComponent<UICopySweepEvent>();
            sweepItemGrid.transform.localPosition = pos;

            count = int.Parse( options["count"] );
            chapterId = options["chapterId"];
            levelId = options["levelId"];

            titleAlpha.ResetToBeginning();
            titleAlpha.PlayForward();

            titlePosition.ResetToBeginning();
            titlePosition.PlayForward();

            bodyScale.ResetToBeginning();
            bodyScale.PlayForward();

            rotateAlpha.ResetToBeginning();
            rotateAlpha.PlayForward();
            openedLevel = StaticData.playerData.level;
            if (items.Count > 0)
            {
                foreach (var item in items)
                {
                    GameObject.DestroyImmediate(item);
                }

                items.Clear();
            }
            sweepScrollView.ResetPosition();
            OnSweepCopyAuto(0);
            //StartCoroutine(_StartSweep(0));
        }

        public bool needStop = false;

        public void _ShowBtn(bool isStop)
        {
            //btn.SetActive( true );

            stopBtn.SetActive(isStop);
            submitBtn.SetActive(!isStop);
        }

        IEnumerator _StartSweep(int current)
        {

            if (current >= count)
            {
                _ShowBtn(false);
                yield break;
            }

            if(needStop)
            {
                _ShowBtn(false);
                yield break;
            }
            

            yield return new WaitForSeconds(titleAlpha.duration + bodyScale.duration);
            _ShowBtn(true);
            OnSweepCopyAuto(current);
        }
        

        public void OnSweepCopyAuto(int times)
        {
            var copySweep = new CSCopySweep();

            copySweep.chapterId = chapterId;
            copySweep.missionId = levelId;

            NetAPI.SendHttp(OpDefine.CSCopySweep, copySweep, (data, ok) =>
            {
                if (ok)
                {
                    SCCopySweep sweepObj = (SCCopySweep)data.kBody;

                    StaticData.playerData.AddResultObj(sweepObj.gain);


                    var level = UICopyData.GetUILevelData(GetComponent<UICopySweep>().levelId);
                    var powerCount = level.level_power_cost;

                    StaticData.playerData.Stamina -= powerCount;//

                    UICopyData.AddCurrentLevelChallengeCount(GetComponent<UICopySweep>().levelId, 1);

                    GetComponent<UICopySweep>().Refresh(sweepObj, times);

                    if (!string.IsNullOrEmpty(sweepObj.wantedId))
                    {
                        _ShowBtn(false);
                        BoxManager.CreateMeetBossBox(sweepObj.wantedId);
                    }
                    else
                    {
                        StartCoroutine(_StartSweep(times + 1));
                    }
                }
                else
                {
                    _ShowBtn(false);
                }
            });
        }
        protected override void DoClose()
        {
            StopAllCoroutines(); 
            if( items.Count>0 )
            {
                foreach( var item in items )
                {
                    GameObject.DestroyImmediate( item );
                }

                items.Clear();
            }
            if(openedLevel < StaticData.playerData.level)
            {
                BoxManager.CreateLevelUp(openedLevel, StaticData.playerData.level);
            }
        }

        public void Refresh(object obj,int times)
        {
            SCCopySweep sweep = (SCCopySweep)obj;
            //StartCoroutine( _Refresh(sweep.gain,times) );

            var it = sweep.gain;

            GameObject inst = GameObject.Instantiate(sweepItem) as GameObject;
           
            inst.SetActive(true);
            inst.transform.SetParent(sweepItemGrid.transform);
            inst.transform.localScale = Vector3.one;

            var item = inst.GetComponent<UICopySweepItem>();

            var parser = new UIDropSimpleParser(it);

            item.Init(parser, ++times);
            inst.name = times.ToString();
            item.alpha.ResetToBeginning();
            item.alpha.PlayForward();

            items.Add(inst);
            sweepItemGrid.Reposition();
            TweenPosition tp = inst.GetComponent<TweenPosition>();
            tp.from = new Vector3(-500, inst.transform.localPosition.y, 0);
            tp.to = new Vector3(0, tp.from.y, 0);
            tp.ResetToBeginning();
            tp.PlayForward();

            if (times > 2)
            {
                MoveUpPosition(0.2f);
            }
            
        }

        List<GameObject> items = new List<GameObject>();

        void MoveUpPosition(float time )
        {
            Vector3 p = sweepItemGrid.transform.localPosition;
            p.y += sweepItemGrid.cellHeight;
            TweenPosition.Begin(sweepItemGrid.gameObject, time, p);
        }
    }
}